home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / DIINFO.C < prev    next >
C/C++ Source or Header  |  1993-08-11  |  12KB  |  378 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    diinfo.c
  5. //   Title:    Data File I/O Library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains to read and write informational fields to a data file.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <di.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //   Description:    Get number of blocks in a logical file.
  49. //    Parameters:    hlf                Logical file handle.
  50. //                        pusBlocks        Number of blocks in file.
  51. //       Returns:    TRUE if successful.
  52. //----------------------------------------------------------------------------
  53. BOOL FN_E DioGetBlocks(HLF hlf, PLONG plBlocks)
  54. {
  55.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  56.     Assert(di.logical[hlf].fUsed);
  57.     Assert(plBlocks);
  58.     Assert((di.logical[hlf].flen % (FPOS)di.logical[hlf].usBlockSize) == 0);
  59.     *plBlocks = (LONG)(di.logical[hlf].flen / (FPOS)di.logical[hlf].usBlockSize);
  60.     return TRUE;
  61. }
  62.  
  63.  
  64. //----------------------------------------------------------------------------
  65. //   Description:    Get block size of a logical file.
  66. //    Parameters:    hlf                Logical file handle.
  67. //                        pusBlockSize    Variable to receive block size.
  68. //       Returns:    TRUE if successful.
  69. //----------------------------------------------------------------------------
  70. BOOL FN_E DioGetBlockSize(HLF hlf, PUSHORT pusBlockSize)
  71. {
  72.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  73.     Assert(di.logical[hlf].fUsed);
  74.     Assert(pusBlockSize);
  75.     *pusBlockSize = di.logical[hlf].usBlockSize;
  76.     return TRUE;
  77. }
  78.  
  79.  
  80. //----------------------------------------------------------------------------
  81. //   Description:    Get flags for a logical file.
  82. //    Parameters:    hlf                Logical file handle.
  83. //                        pfl                Variable to receive flags
  84. //       Returns:
  85. //----------------------------------------------------------------------------
  86. BOOL FN_E DioGetFlags(HLF hlf, PFLAG32 pfl)
  87. {
  88.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  89.     Assert(di.logical[hlf].fUsed);
  90.     Assert(pfl);
  91.     *pfl = di.logical[hlf].fl;
  92.     return TRUE;
  93. }
  94.  
  95.  
  96. //----------------------------------------------------------------------------
  97. //   Description:    Get # of records in a compressed isam data file.
  98. //    Parameters:    hlf        Logical file handle.
  99. //                        pl            Number of records in the file.
  100. //       Returns:
  101. //----------------------------------------------------------------------------
  102. BOOL FN_E DioGetRecords(HLF hlf, PLONG pl)
  103. {
  104.     DA_COMPRESS_USER da_compress_user;
  105.  
  106.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  107.     Assert(di.logical[hlf].fUsed);
  108.     Assert(di.logical[hlf].usType == DFT_ISAM_DATA);
  109.     Assert(pl);
  110.     *pl = 0;
  111.  
  112.     if (!DioGetUser(hlf, (PBYTE)&da_compress_user, sizeof(da_compress_user)))
  113.         return FALSE;
  114.  
  115.     *pl = da_compress_user.lRecs;
  116.     return TRUE;
  117. }
  118.  
  119.  
  120. //----------------------------------------------------------------------------
  121. //   Description:    Get size of a logical file.
  122. //    Parameters:    hlf                Logical file handle.
  123. //                        pfsize            Variable to receive flags
  124. //       Returns:
  125. //----------------------------------------------------------------------------
  126. BOOL FN_E DioGetSize(HLF hlf, PFPOS pfsize)
  127. {
  128.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  129.     Assert(di.logical[hlf].fUsed);
  130.     Assert(pfsize);
  131.     *pfsize = di.logical[hlf].flen;
  132.     return TRUE;
  133. }
  134.  
  135.  
  136. //----------------------------------------------------------------------------
  137. //   Description:    Get user data for a logical file.
  138. //    Parameters:    hlf        Logical file handle.
  139. //                        pb            Buffer to receive data.
  140. //                        cb            Size of buffer
  141. //       Returns:
  142. //----------------------------------------------------------------------------
  143. BOOL FN_E DioGetUser(HLF hlf, PBYTE pb, SIZET cb)
  144. {
  145.     DATADIR dir;
  146.     HPF hpf;
  147.     SIZET cDir;
  148.  
  149.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  150.     Assert(di.logical[hlf].fUsed);
  151.     Assert(pb && cb);
  152.     memset(pb, 0, cb);
  153.  
  154.     hpf = di.logical[hlf].hpf;
  155.     cDir = di.logical[hlf].cDir;
  156.  
  157.     if (!DioDirRead(hpf, cDir, &dir))
  158.         return FALSE;
  159.  
  160.     cb = MIN(sizeof(dir.bUser), cb);
  161.     memcpy(pb, dir.bUser, cb);
  162.  
  163.     return TRUE;
  164. }
  165.  
  166.  
  167. //----------------------------------------------------------------------------
  168. //   Description:    Get user data for a logical file.
  169. //    Parameters:    hlf            Logical file handle.
  170. //                        pusVersion    Variable to receive user version.
  171. //       Returns:
  172. //----------------------------------------------------------------------------
  173. BOOL FN_E DioGetVersion(HLF hlf, PUSHORT pusVersion)
  174. {
  175.     DATADIR dir;
  176.     HPF hpf;
  177.     SIZET cDir;
  178.  
  179.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  180.     Assert(di.logical[hlf].fUsed);
  181.     Assert(pusVersion);
  182.  
  183.     hpf = di.logical[hlf].hpf;
  184.     cDir = di.logical[hlf].cDir;
  185.  
  186.     if (!DioDirRead(hpf, cDir, &dir))
  187.         return FALSE;
  188.  
  189.     *pusVersion = dir.usUserVersion;
  190.  
  191.     return TRUE;
  192. }
  193.  
  194.  
  195. //----------------------------------------------------------------------------
  196. //   Description:    Get flags for physical file.
  197. //    Parameters:    hpf        Physical file handle
  198. //                        pfl        Variable to receive flags
  199. //       Returns:    TRUE if successful.
  200. //----------------------------------------------------------------------------
  201. BOOL FN_E DioHeaderGetFlags(HPF hpf, PFLAG32 pfl)
  202. {
  203.     Assert(hpf >= 0 && hpf < MAX_PHYSICAL_FILES);
  204.     Assert(di.physical[hpf].fUsed);
  205.     Assert(pfl);
  206.     *pfl = di.physical[hpf].fl;
  207.     return TRUE;
  208. }
  209.  
  210.  
  211. //----------------------------------------------------------------------------
  212. //   Description:    Get user data from file header
  213. //    Parameters:    hpf        Physical file handle.
  214. //                        pb            Data buffer
  215. //                        cb            Size of buffer
  216. //       Returns:    TRUE if successful.
  217. //----------------------------------------------------------------------------
  218. BOOL FN_E DioHeaderGetUser(HPF hpf, PBYTE pb, SIZET cb)
  219. {
  220.     DATAHDR hdr;
  221.  
  222.     Assert(pb && cb);
  223.     if (!DioHeaderRead(hpf, &hdr))
  224.         return FALSE;
  225.     cb = MIN(sizeof(hdr.bUser), cb);
  226.     memcpy(pb, hdr.bUser, cb);
  227.     return TRUE;
  228. }
  229.  
  230.  
  231. //----------------------------------------------------------------------------
  232. //   Description:    Get user version from header
  233. //    Parameters:    hpf            Physical file handle.
  234. //                        pusVersion    Variable to receive user version.
  235. //       Returns:    TRUE if successful.
  236. //----------------------------------------------------------------------------
  237. BOOL FN_E DioHeaderGetVersion(HPF hpf, PUSHORT pusVersion)
  238. {
  239.     DATAHDR hdr;
  240.  
  241.     Assert(pusVersion);
  242.     if (!DioHeaderRead(hpf, &hdr))
  243.         return FALSE;
  244.     *pusVersion = hdr.usUserVersion;
  245.     return TRUE;
  246. }
  247.  
  248.  
  249. //----------------------------------------------------------------------------
  250. //   Description:    Set flags in file header
  251. //    Parameters:    hpf        Physical file handle
  252. //                        fl            Flags.
  253. //       Returns:    TRUE if successful.
  254. //----------------------------------------------------------------------------
  255. BOOL FN_E DioHeaderSetFlags(HPF hpf, FLAG32 fl)
  256. {
  257.     DATAHDR hdr;
  258.  
  259.     if (!DioHeaderRead(hpf, &hdr))
  260.         return FALSE;
  261.     hdr.fl &= 0x0000FFFFL;                    // Set new flags
  262.     hdr.fl |= (fl & 0xFFFF0000L);
  263.     return DioHeaderWrite(hpf, &hdr);
  264. }
  265.  
  266.  
  267. //----------------------------------------------------------------------------
  268. //   Description:    Set header user data.
  269. //    Parameters:    hpf        Physical file handle
  270. //                        pb            Data buffer
  271. //                        cb            Size of buffer
  272. //       Returns:    TRUE if successful.
  273. //----------------------------------------------------------------------------
  274. BOOL FN_E DioHeaderSetUser(HPF hpf, PBYTE pb, SIZET cb)
  275. {
  276.     DATAHDR hdr;
  277.  
  278.     Assert(pb && cb);
  279.     if (!DioHeaderRead(hpf, &hdr))
  280.         return FALSE;
  281.     cb = MIN(sizeof(hdr.bUser), cb);
  282.     memcpy(hdr.bUser, pb, cb);
  283.     return DioHeaderWrite(hpf, &hdr);
  284. }
  285.  
  286.  
  287. //----------------------------------------------------------------------------
  288. //   Description:    Set header user version.
  289. //    Parameters:    hpf            Physical file handle
  290. //                        usVersion    User version
  291. //       Returns:    TRUE if successful.
  292. //----------------------------------------------------------------------------
  293. BOOL FN_E DioHeaderSetVersion(HPF hpf, USHORT usVersion)
  294. {
  295.     DATAHDR hdr;
  296.  
  297.     if (!DioHeaderRead(hpf, &hdr))
  298.         return FALSE;
  299.     hdr.usUserVersion = usVersion;
  300.     return DioHeaderWrite(hpf, &hdr);
  301. }
  302.  
  303.  
  304. //----------------------------------------------------------------------------
  305. //   Description:    Set flags for logical file.
  306. //    Parameters:    hlf    Logical file handle.
  307. //                        fl        Flags to set.            
  308. //       Returns:    TRUE if successful.
  309. //----------------------------------------------------------------------------
  310. BOOL FN_E DioSetFlags(HLF hlf, FLAG32 fl)
  311. {
  312.     DATADIR dir;
  313.     HPF hpf;
  314.     SIZET cDir;
  315.  
  316.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  317.     Assert(di.logical[hlf].fUsed);
  318.     hpf = di.logical[hlf].hpf;
  319.     cDir = di.logical[hlf].cDir;
  320.     if (!DioDirRead(hpf, cDir, &dir))
  321.         return FALSE;
  322.     dir.fl &= 0x0000FFFFL;                    // Set new flags
  323.     dir.fl |= (fl & 0xFFFF0000L);
  324.     return DioDirWrite(hpf, cDir, &dir);
  325. }
  326.  
  327.  
  328. //----------------------------------------------------------------------------
  329. //   Description:    Set user data for a logical file.
  330. //    Parameters:    hlf        Logical file handle.
  331. //                        pb            Data buffer
  332. //                        cb            Size of buffer
  333. //       Returns:    TRUE if successful.
  334. //----------------------------------------------------------------------------
  335. BOOL FN_E DioSetUser(HLF hlf, PBYTE pb, SIZET cb)
  336. {
  337.     DATADIR dir;
  338.     HPF hpf;
  339.     SIZET cDir;
  340.  
  341.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  342.     Assert(di.logical[hlf].fUsed);
  343.     Assert(pb && cb);
  344.     hpf = di.logical[hlf].hpf;
  345.     cDir = di.logical[hlf].cDir;
  346.     if (!DioDirRead(hpf, cDir, &dir))
  347.         return FALSE;
  348.     cb = MIN(sizeof(dir.bUser), cb);
  349.     memcpy(dir.bUser, pb, cb);
  350.     return DioDirWrite(hpf, cDir, &dir);
  351. }
  352.  
  353.  
  354. //----------------------------------------------------------------------------
  355. //   Description:    Set user version
  356. //    Parameters:    hlf            Logical file handle.
  357. //                        usVersion    User version
  358. //       Returns:    TRUE if successful.
  359. //----------------------------------------------------------------------------
  360. BOOL FN_E DioSetVersion(HLF hlf, USHORT usVersion)
  361. {
  362.     DATADIR dir;
  363.     HPF hpf;
  364.     SIZET cDir;
  365.  
  366.     Assert(hlf >= 0 && hlf < MAX_LOGICAL_FILES);
  367.     Assert(di.logical[hlf].fUsed);
  368.     hpf = di.logical[hlf].hpf;
  369.     cDir = di.logical[hlf].cDir;
  370.     if (!DioDirRead(hpf, cDir, &dir))
  371.         return FALSE;
  372.     dir.usUserVersion = usVersion;
  373.     return DioDirWrite(hpf, cDir, &dir);
  374. }
  375. //----------------------------------------------------------------------------
  376. //------------------------------- End of File --------------------------------
  377. //----------------------------------------------------------------------------
  378.